home *** CD-ROM | disk | FTP | other *** search
/ AOL File Library: 2,801 to 2,900 / aol-file-protocol-4400-2801-to-2900.zip / AOLDLs / C++ Files Library / SK (Sockects) 1.4.1 r2 / SK v1.4.1 r2.sit / SK 1.4.1 r2 / SK / SK Sources / SK_Buffer.cc next >
Text File  |  1994-06-13  |  6KB  |  245 lines

  1. /***************************************************************************
  2. * Copyright ⌐ 1992-1994 Matthias Neeracher and the Decision Systems Group
  3. * Permission is granted to anyone to use this software for any purpose on
  4. * any computer system, and to redistribute it freely, subject to the
  5. * following restrictions:
  6. * 1) The authors and the Decision Systems Group are not responsible for 
  7. *    the direct or indirect consequences of use of this software, no matter 
  8. *    how awful, even if they arise from defects in the software itself.
  9. *    This restriction applies to the use of this and any derived source code 
  10. *    and also to the use of all binary produced from this and any derived 
  11. *    source code.
  12. * 2) The origin and copyrights of this software must not be misrepresented, 
  13. *    either by explicit claim or by omission or alteration of copyright or
  14. *    authorship header information in this file or in any derived file.
  15. * 3) Altered or derived versions must be plainly marked as such, and must not
  16. *    be misrepresented as being the original software.
  17. * We encourage users of this software to provide feedback, bug fixes,
  18. * and enhancements to the authors for incorporation into future releases.
  19. *
  20. * ==========================================================================
  21. * FILE: SK_Buffer.cc
  22. * AUTHOR: Matthias Neeracher and Stephan Deibel
  23. * CREATION DATE: 01Aug92
  24. * VERSION: 13Jun94
  25. * DESCRIPTION: 
  26. * Scatterer/Gatherer (Producer/Consumer) classes for SK buffering
  27. * NOTES: 
  28. * 0) This code was derived from Matthias Neerarcher's GUSI 1.1.0; Copyrights
  29. *    are subject to that fact.
  30. *
  31. * MODIFICATIONS: 
  32. * --------------------------------------------------------------------------
  33. * Date     Name      Description of modification
  34. * --------------------------------------------------------------------------
  35. * 02Aug92   MN       SKt_Deferred procedures
  36. * 23Aug92    MN       Optimize buffer size for empty buffers
  37. * 10Feb93    MN       The above was incorrect, next attempt
  38. * 13Jul93    SD       Port to THINK C++ 6.0
  39. * 21Jul93    SD       Subsetted to only TCP/UDP, applied DSG naming 
  40. *                    conventions and commenting guidlines, and
  41. *                    changed name to "SK" for clear distinction from
  42. *                    the GUSI code from which this was derived
  43. * 13Jun94   SD       Updated based on relevant changes to GUSI (through 1.4.1)
  44. */
  45.  
  46. #ifndef SK_BUFFER_DEBUG
  47. #define NDEBUG
  48. #endif
  49.  
  50. #include "SK_P.hh"
  51.  
  52.  
  53.  
  54. /**************************************************************************/ 
  55. /* Scatter/Gather Generic Class */
  56.  
  57.  
  58. /****************************************************************************
  59. * FUNCTION:
  60. *
  61. * CSK_ScattGath::CSK_ScattGath()
  62. *
  63. * DESCRIPTION:
  64. *
  65. * Constructor for scatterer/gatherer generic class
  66. *
  67. * PARAMETERS:
  68. *
  69. * struct iovec *        -- I/O vector
  70. * int                     -- Count
  71. *
  72. */
  73.  
  74. CSK_ScattGath::CSK_ScattGath(const struct iovec *iov, int cnt)    
  75. {
  76.     fIOVector        =    iov;
  77.     fCount    =    cnt;
  78.  
  79.     if (fCount < 1)    {
  80.         fBuffer        =    nil;
  81.         fLength        =    0;
  82.         fScratchHandle    =    nil;
  83.     } else if (fCount == 1)    {    
  84.         fBuffer        =    (void *) iov->iov_base;
  85.         fLength        =    (int)    iov->iov_len;
  86.         fScratchHandle    =    nil;
  87.     } else {
  88.         for (fLength = 0; fCount--; ++iov)
  89.             fLength += (int) iov->iov_len;
  90.         
  91.         fScratchHandle = NewHandle(fLength);
  92.         
  93.         if (fScratchHandle)    {
  94.             MoveHHi(fScratchHandle);
  95.             HLock(fScratchHandle);
  96.             fBuffer    = (void *) *fScratchHandle;
  97.         } else
  98.             fBuffer = nil;
  99.     }
  100. }
  101.  
  102.  
  103. /****************************************************************************
  104. * FUNCTION:
  105. *
  106. * CSK_ScattGath::~CSK_ScattGath()
  107. *
  108. * DESCRIPTION:
  109. *
  110. * Destructor for scatterer/gatherer generic class
  111. *
  112. */
  113.  
  114. CSK_ScattGath::~CSK_ScattGath()
  115. {
  116.     if (fScratchHandle) {
  117.         HUnlock(fScratchHandle);
  118.         fBuffer = nil;
  119.         DisposHandle(fScratchHandle);
  120.         }
  121. }
  122.  
  123.  
  124.  
  125. /**************************************************************************/ 
  126. /* Scatterer Class */
  127.  
  128.  
  129. /*************************************************************************** *
  130. * FUNCTION:
  131. *
  132. * CSK_Scatterer::CSK_Scatterer()
  133. *
  134. * DESCRIPTION:
  135. *
  136. * Constructor for scatterer class
  137. *
  138. * PARAMETERS:
  139. *
  140. * struct iovec *        -- I/O vector
  141. * int                     -- Count
  142. *
  143. */
  144.  
  145.  
  146. CSK_Scatterer::CSK_Scatterer(const struct iovec *iov, int count) 
  147.     : CSK_ScattGath(iov, count)
  148. {
  149. }
  150.  
  151.  
  152. /****************************************************************************
  153. * FUNCTION:
  154. *
  155. * CSK_Scatterer::~CSK_Scatterer()
  156. *
  157. * DESCRIPTION:
  158. *
  159. * Destructor for scatterer class
  160. *
  161. */
  162.  
  163. CSK_Scatterer::~CSK_Scatterer()
  164. {
  165.     int    sect;
  166.     
  167.     if (fCount > 1 && fBuffer)
  168.         for (char * bptr = (char *) fBuffer; fCount-- && fLength; ++fIOVector)    {
  169.             sect = MIN(fLength, fIOVector->iov_len);
  170.             
  171.             memcpy(fIOVector->iov_base, bptr, sect);
  172.             
  173.             bptr += sect;
  174.             fLength -= sect;
  175.         }
  176. }
  177.  
  178.  
  179.  
  180.  
  181. /**************************************************************************/ 
  182. /* Gatherer Class */
  183.  
  184.  
  185. /*************************************************************************** *
  186. * FUNCTION:
  187. *
  188. * CK_Gatherer::CK_Gatherer()
  189. *
  190. * DESCRIPTION:
  191. *
  192. * Constructor for gatherer class
  193. *
  194. * PARAMETERS:
  195. *
  196. * struct iovec *        -- I/O vector
  197. * int                     -- Count
  198. *
  199. */
  200.  
  201.  
  202. CK_Gatherer::CK_Gatherer(const struct iovec *iov, int count) 
  203.     : CSK_ScattGath(iov, count)
  204. {
  205.     if (fCount > 1 && fBuffer)
  206.         for (char * bptr = (char *) fBuffer; fCount--; ++iov)    {
  207.             memcpy(bptr, iov->iov_base, iov->iov_len);
  208.             
  209.             bptr += iov->iov_len;
  210.         }
  211. }
  212.  
  213.  
  214. /****************************************************************************
  215. * FUNCTION:
  216. *
  217. * CK_Gatherer::~CK_Gatherer()
  218. *
  219. * DESCRIPTION:
  220. *
  221. * Destructor for gatherer class
  222. *
  223. */
  224.  
  225. CK_Gatherer::~CK_Gatherer()
  226. {
  227. }
  228.  
  229.  
  230.